//
//  InteractivePopAnimator.m
//  FFStory
//
//  Created by BaeCheung on 14/11/12.
//  Copyright (c) 2014年 FF. All rights reserved.
//

#import "InteractivePopAnimator.h"
#import <UIKit/UIKit.h>

UIViewAnimationOptions const TSNavigationTransitionCurve = 7 << 16;

@implementation UIView (TransitionShadow)
- (void)addLeftSideShadowWithFading
{
    CGFloat shadowWidth   = 4.0f;
    CGFloat shadowPadding = 20.0f;
    CGRect shadowRect = self.frame;
    shadowRect.origin.x = -shadowWidth;   shadowRect.size.width   = shadowWidth;
    shadowRect.origin.y = -shadowPadding; shadowRect.size.height += shadowPadding * 2;
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:shadowRect];
    self.layer.shadowPath    = [shadowPath CGPath];
    
    CGFloat fromValue = 0.2f;
    CGFloat toValue   = 0.0f;
    self.layer.shadowOpacity = fromValue;
    
    // fade shadow during transition
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
    animation.fromValue = @(fromValue);
    animation.toValue   = @(toValue);
    animation.removedOnCompletion = NO;
    [self.layer addAnimation:animation forKey:nil];
}
@end


@interface InteractivePopAnimator()
@property (weak, nonatomic) UIViewController *toViewController;
@end

@implementation InteractivePopAnimator

// Approximated lengths of the default animations.
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    return [transitionContext isInteractive] ? 0.25f : 0.5f;
}

// Tries to animate a pop transition similarly to the default iOS' pop transition.
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *toViewController   = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];
    
    // parallax effect; the offset matches the one used in the pop animation in iOS 7.1
    CGFloat toViewControllerXTranslation = - CGRectGetWidth([transitionContext containerView].bounds) * 0.3f;
    toViewController.view.transform = CGAffineTransformMakeTranslation(toViewControllerXTranslation, 0);
    
    // add a shadow on the left side of the frontmost view controller
    [fromViewController.view addLeftSideShadowWithFading];
    BOOL previousClipsToBounds = fromViewController.view.clipsToBounds;
    fromViewController.view.clipsToBounds = NO;
    
    // in the default transition the view controller below is a little dimmer than the frontmost one
    UIView *dimmingView = [[UIView alloc] initWithFrame:toViewController.view.bounds];
    dimmingView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.1f];
    [toViewController.view addSubview:dimmingView];
    
    // Uses linear curve for an interactive transition, so the view follows the finger. Otherwise, uses a navigation transition curve.
    UIViewAnimationOptions curveOption = [transitionContext isInteractive] ? UIViewAnimationOptionCurveLinear : TSNavigationTransitionCurve;
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionTransitionNone | curveOption animations:^{
        toViewController.view.transform = CGAffineTransformIdentity;
        fromViewController.view.transform = CGAffineTransformMakeTranslation(toViewController.view.frame.size.width, 0);
        dimmingView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [dimmingView removeFromSuperview];
        fromViewController.view.transform = CGAffineTransformIdentity;
        fromViewController.view.clipsToBounds = previousClipsToBounds;
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        
    }];
    
    self.toViewController = toViewController;
}

// restore the toViewController's transform if the animation was cancelled
- (void)animationEnded:(BOOL)transitionCompleted {
    if (!transitionCompleted) {
        self.toViewController.view.transform = CGAffineTransformIdentity;
    }
}

@end
